import json

import pymysql
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

try:
    connection = pymysql.connect(
        host='localhost',
        user='root',
        password='zhang77is',
        db='当当网',
        charset='utf8mb4',
        cursorclass=pymysql.cursors.DictCursor
    )
    if connection.open:
        print("Connected to the database")
        cursor = connection.cursor()
except Exception as e:
    print(f'Error while connecting')

finally:
    if connection:
        connection.close()
        print("Connection closed")


def get_news_info(page=1):
    # 设置 ChromeDriver 路径
    chrome_driver_path = "/usr/local/bin/chromedriver"
    # 配置 Chrome 选项
    options = Options()
    options.add_argument("--disable-blink-features=AutomationControlled")

    driver = webdriver.Chrome(service=Service(executable_path=chrome_driver_path), options=options)

    driver.get("http://www.ahsczx.com/Welcome/NewsList.aspx?NewsClassID=17&FaterID=16&page=" + str(page))

    wait = WebDriverWait(driver, 3)
    # print(driver.page_source)

    span = driver.find_element(By.XPATH, '//*[@id="AspNetPager1"]/table/tbody/tr/td[1]/span[1]')
    print(span.text)
    # 查找DataListAll标签
    data_list_all = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'DataListAll'))
    )

    # 查找表格中的所有<li>标签
    li_tags = data_list_all.find_elements(By.TAG_NAME, 'li')

    for li in li_tags:
        a = li.find_element(By.TAG_NAME, 'a')
        href = a.get_attribute('href')
        print('---')
        driver.execute_script("window.open('');")
        driver.switch_to.window(driver.window_handles[-1])
        # 访问超链接
        driver.get(href)
        # 等待页面加载完成
        wait = WebDriverWait(driver, 1)
        shownewstitle = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'shownewstitle'))).text
        shownewdate = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'shownewdate'))).text
        shownewscontent = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'shownewscontent'))).text

        print(f'shownewstitle: {shownewstitle}')
        print(f'shownewdate: {shownewdate}')
        print(f'Shownewscontent: {shownewscontent}')
        # 关闭当前标签页
        driver.close()
        driver.switch_to.window(driver.window_handles[0])

    driver.quit()

# if __name__ == '__main__':
# for i in range(2):
#     get_news_info(i)
